package message

import (
	"encoding/xml"
	"github.com/revel/revel"
)

/*

！首次注册用户 请求消息
INFO  2017/09/03 20:32:39 wx.go:34: <xml><ToUserName><![CDATA[gh_824d2a215c6e]]></ToUserName>
<FromUserName><![CDATA[oJyUL1t0s_cLT4Cc-oKw6OdgbLhw]]></FromUserName>
<CreateTime>1504441959</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[subscribe]]></Event>
<EventKey><![CDATA[]]></EventKey>
</xml>
INFO  2017/09/03 20:32:39 wx.go:57:   <xml>
      <ToUserName><![CDATA[oJyUL1t0s_cLT4Cc-oKw6OdgbLhw]]></ToUserName>
      <FromUserName><![CDATA[gh_824d2a215c6e]]></FromUserName>
      <CreateTime>1504441959</CreateTime>
      <MsgType><![CDATA[text]]></MsgType>
      <Content><![CDATA[resp: ]]></Content>
  </xml>

*/

/*

点击

<FromUserName><![CDATA[oJyUL1vz6W-nm41qaihmLMtuKAi4]]></FromUserName>
<CreateTime>1504446266</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[CLICK]]></Event>
<EventKey><![CDATA[V1001_SALE]]></EventKey>
</xml>

*/

type PushMsg struct {
	ToUserName   string `xml:"ToUserName"`
	FromUserName string `xml:"FromUserName"`
	CreateTime   string `xml:"CreateTime"`
	MsgType      string `xml:"MsgType"`
	Event        string `xml:"Event"`
	Content      string `xml:"Content"`
	MsgId        string `xml:"MsgId"`
	EventKey     string `xml:"EventKey"`
}

func ParseMsg(str string) *PushMsg {
	v := &PushMsg{}
	err := xml.Unmarshal([]byte(str), v)
	if err != nil {
		revel.ERROR.Println(err, "parse push message error")
		return nil
	}
	return v
}

type CDDATA struct {
	V string `xml:",cdata"`
}

type RspMsg struct {
	XMLName      xml.Name `xml:"xml"`
	ToUserName   *CDDATA
	FromUserName *CDDATA
	CreateTime   string
	MsgType      *CDDATA
	Content      *CDDATA
}
